layui.use('table', function () {
    var table = layui.table;
    table.render({
        elem: '#demo',
        url: '/info/userCount/list',
        toolbar: '#toolbar',
        page: true,
        cols: [[
            {type: 'checkbox'},
            {type: 'numbers',title: '序号'},
            {field: 'num',title: '账号',sort: true},
            {field: 'name',title: '姓名',sort: true},
            {field: 'password', title: '密码'},
            {field: 'type',title: '身份',sort: true},
            {field: 'operation',title: '操作',toolbar: '#rowTools',width:220}
        ]],
        height: 'full-20'
    });
    // 行工具栏监听
    table.on('tool(test)', function (obj) {
        switch (obj.event) {
            case 'edit':
                layer.prompt({
                    title: '修改 '+obj.data.name+' 的密码',
                    formType: 2,
                    value: obj.data.password
                }, function (value, index) {
                    // 服务器修改
                    $.ajax({
                        url: '/info/userCount/update',
                        type: 'post',
                        data: {num: obj.data.num,password: value},
                        success: function (params) {
                            if (params.affectedRows > 0) {
                                layer.msg('密码修改成功,已修改' + params.affectedRows +'数据!');
                                // 同步更新表格
                                obj.update({password: value})
                            }else{
                                layer.msg('密码修改失败');
                            }
                            layer.close(index);
                        }
                    });
                });
                break;
            case 'del':
                layer.confirm('真的删除该账户吗?', function (index) {
                    // 向服务器发送数据(删除指令)
                    $.ajax({
                        url: '/info/userCount/delete',
                        data: {num: obj.data.num},
                        success: function (params) {
                            if (params.affectedRows > 0) {
                                layer.msg('成功删除' + params.affectedRows +'数据!');
                                obj.del();
                            } else {
                                layer.msg('删除失败!');
                            }
                            layer.close(index);
                        }
                    });
                });
                break;
            case 'authorize':
                layer.open({
                    title: '设置 '+obj.data.name+' 的权限',
                    type: 1,
                    content: $('.set-type'),
                    btn: ['确认','取消'],
                    yes: function() {
                        var type = $('.stype option:selected').val();
                        $.ajax({
                            url: '/info/userCount/setType',
                            data: {num: obj.data.num, type: type},
                            success: function (params) {
                                if (params.affectedRows > 0) {
                                    obj.update({type: type});
                                    layer.msg('权限设置成功，已修改' + params.affectedRows +'数据!');
                                } else {
                                    layer.msg('设置失败!');
                                }
                            }
                        });
                    }
                });
                break;
        }     
    });
    // 头部工具栏监听
    table.on('toolbar(test)', function (obj) {   
        switch (obj.event) {
            case 'searchUser':
                var inputVal = $('.layui-input').val();
                var temp = inputVal;
                if ($.trim(inputVal) == '' || $.trim(inputVal) == null) {
                    return layer.msg('输入不能为空!');
                }
                table.reload('demo', {
                    url: '/info/userCount/search',
                    page: {curr: 1},
                    where: {num: inputVal}
                });
                $('.layui-input').val(temp);
                break;
            case 'refresh':
                reloads();
                break;
            case 'delUsers':
                var checkStatus = table.checkStatus('demo');
                var len = checkStatus.data.length;
                if (len <= 0) {
                    return layer.msg('请先选择要删除的数据!');
                }
                var strNums = [];
                for (var i = 0; i < len; i++) {
                    strNums.push(checkStatus.data[i].num);
                }
                var nums = strNums.join(',');
                layer.confirm('真的删除吗?', function (index) {
                    // 向服务器发送数据(删除指令)
                    $.ajax({
                        url: '/info/userCount/delete',
                        data: {num: nums},
                        success: function(params) {
                            if (params.affectedRows > 0) {
                                layer.msg('成功删除' + params.affectedRows + '数据!')
                                reloads();
                            } else {
                                layer.msg('删除失败!')
                            }
                        }
                    });
                });
                break;
            case 'addUser':
                layer.open({
                    title: $(this).text(),
                    type: 1,
                    content: $('.form')
                });
                break;
        }
    });
    $('.submit').click(function() {
        var num = $('.num').val();
        var name = $('.name').val();
        var pwd = $('.pwd').val();
        var type = $('.type option:selected').val();
        $.ajax({
            url: '/info/userCount/add',
            type: 'POST',
            data: {num: num,name: name,pwd: pwd,type: type},
            success: function (params) {
                if (params.affectedRows > 0) {
                    layer.msg('已成功添加' + params.affectedRows +'数据!');
                    reloads();
                } else if (params.error){
                    layer.msg(params.msg);
                }
            }
        });
    });
    function reloads(){
        table.reload('demo', {
            url: '/info/userCount/list'
        });
    }
});